
package com.google.unizone.server;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jdo.annotations.PersistenceCapable;  
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.restfb.ExtendedFaceBookClient;

@PersistenceCapable(detachable = "true")//(identityType = IdentityType.APPLICATION, detachable = "true")
public class Student {

	@PrimaryKey
	@Persistent
	private String facebookID;
	@Persistent
	private String accessToken;
	@Persistent
	private String picUrl;
	@Persistent
	private int isFree = 0;
	@Persistent
	private String name;
	@Persistent
	private String faculty;
	@Persistent
	private int year; 
	@Persistent
	private Set<String> courseIDList;
	@Persistent
	private Set<String> fbFriends;

	public Student (String facebookID)
	{
		this.facebookID = facebookID;
		this.courseIDList = new TreeSet<String>();
		this.fbFriends = new TreeSet<String>();
	}
	
	
	public Student(String facebookID, String accessToken, String picUrl, String friends, String name, String faculty, int year, String courseIDList) {
		this.faculty = faculty;
		this.name = name;
		this.year = year;
		this.courseIDList = new TreeSet<String>(Arrays.asList(courseIDList.split(" ")));
		this.accessToken = accessToken;
		this.facebookID = facebookID;
		this.picUrl = picUrl;
		this.fbFriends = new TreeSet<String>(Arrays.asList(friends.split(" ")));
		//str_to_time_table(timeTableStr, timeTable);
	}

	//private void str_to_time_table(String timeTableStr, HashMap<String, String> timeTable) {
	//TODO: decide a format in which will come and parse it
	//}

	public int isFree() {
		return this.isFree;
	}

	//public int isFriendFree(IStudent friend) {
	//return friend.isFree();
	//}

	/* 0 - free
	 * 1 - free but 'taken'
	 * 2 - not free
	 * 3 - not in university
	 */
	public void setFree(int free_type) throws Exception {
		if (free_type > 3 || free_type < 0) {
			Exception up =  new Exception("ERROR : Value of free_type must be an integet 0-3\nC");
			throw up;
		}
		this.isFree = free_type;
	}
	
	public String extractData (String data) {
		Pattern name = Pattern.compile("name\":\"(.*)\",");
		Pattern pa = Pattern.compile("pic_square\":\"(.*)\"");
		Matcher matcher1, matcher2;
		String temp;
		String ret = "";

		try
		{
		matcher1 = pa.matcher(data);
		matcher1.find();
		temp = matcher1.group(1);
		ret += "<img src=\"" + temp + "\">"; 
		
		matcher2 = name.matcher(data);
		matcher2.find();
		temp = matcher2.group(1);
		ret += "<br>" + temp;
		}
		catch(IllegalStateException ex)
		{
			ret = ret + "";
		}
		
		return ret;
	}
	
	public String GetFacebookMutual(List<String> friends){
		StringBuilder sb = new StringBuilder();
		sb.append("<table><tr>");
		for (String friend : friends) {
			sb.append("<td>" + extractData(friend) + "</td>");
		}
		sb.append("</tr></table>");
		//return getMutualFriends(meStudent);
		//return getMutualFriends(meStudent);
		return sb.toString();
	}
	
	
	public String getFacebookID() {
		return this.facebookID;
	}
	public String getAccessToken() {
		return this.accessToken;
	}
	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFaculty() {
		return this.faculty;
	}
	public void setFaculty(String faculty) {
		this.faculty = faculty;
	}
	public int getYear() {
		return this.year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public Set<String> getCourseIDList() {
		return this.courseIDList;
	}
	
	public void setCourseIDList(Set<String> courseIDList) {
		this.courseIDList=courseIDList;
	}
	
	public void addCourseID(String courseID) {
		if (!this.isTakeCourse(courseID)) {
			this.courseIDList.add(courseID);
		}
	}
	public void removeCourseID(String courseID) {
		if (this.isTakeCourse(courseID)) {
			this.courseIDList.remove(courseID);
		}
	}
	public boolean isTakeCourse(String courseID) {
		return this.courseIDList.contains(courseID);
	}
	public String getPic() {
		return this.picUrl;
	}
	public void setPic(String url) {
		this.picUrl = url;
	}
	
	public Set<String> getFBFriends() {
		return this.fbFriends;
	}
	public void setFBFriends(Set<String> friendsIDs) {
		this.fbFriends = friendsIDs;
	}
	
	public String toString(boolean isHeader)
	{
		String link2FacebookS = "<a target=\"_blank\" href=\"http://www.facebook.com/" + this.getFacebookID() +"\">";
		String closeA = "</a>";
		String link2FriendS = "<a href=friendPage.jsp?studentID=" + this.getFacebookID() +">";
		String facePic = "<img src=\"" + this.getPic() + "\">";
		String SchedulePic = "<img src=\"schedule-icon-fs.jpg\">"; 
		String StudentName = this.getName();
		String str = "";
		str += "<table id=\"student_toString\"><tr>";
		if (isHeader){
			str += Utils.rapHTMLTag(link2FacebookS + facePic + closeA , "td");
			str += Utils.rapHTMLTag(Utils.rapHTMLTag(StudentName, "h2"),"td");
		}
		else
		{
			//if ()
			//https://api.facebook.com/method/friends.getMutualFriends?
			//	target_uid=591867536&
			//	access_token=...
			str += Utils.rapHTMLTag(link2FacebookS + facePic + closeA,"td");
			
			str += "<td id = \"student_name\">" + StudentName + " (" + DB_Logic.checkIfStudentFree(this) + ")</td>" ;
			str += Utils.rapHTMLTag(link2FriendS + SchedulePic + closeA, "td");
		}
		str += "</tr></table>";
		
		return str;
	}
	
	
	
	public String toString (Student meStudent, boolean isHeader, ExtendedFaceBookClient fbclient){
		if (Utils.areFriends(meStudent, this.getFacebookID())){
			return toString(isHeader);
		}
		else {
			return toStringNonFriend(meStudent, fbclient);
		}
	}
	
	
	public  String toStringNonFriend (Student meStudent, ExtendedFaceBookClient fbclient){
		String link2FacebookS = "<a target=\"_blank\" href=\"http://www.facebook.com/" + this.getFacebookID() +"\">";
		String closeA = "</a>"; 
		List<String> mutualFriends = fbclient.getMutualFriends(this);
		String tooltipContent = GetFacebookMutual(mutualFriends);
		String facePic = "<img src=\"" + this.getPic() + "\">";
		String StudentName = this.getName();
		String str = "";
		str += "<table rel=\"htmltooltip\" id=\"student_toString\"><tr>";
		//https://api.facebook.com/method/friends.getMutualFriends?
		//	target_uid=591867536&
		//	access_token=...
		str += Utils.rapHTMLTag(link2FacebookS + facePic + closeA,"td");
		str += Utils.rapHTMLTag(StudentName, "td");
		str += Utils.rapHTMLTag("(" + mutualFriends.size() + " mutual friends)", "td");
		str += "</tr></table>";
		str += "<div class=\"htmltooltip\">" + tooltipContent + "</div>";
		
		return str;
		
	}
	
	public Boolean equals(Student student)
	{
		return this.getFacebookID().equals(student.getFacebookID());
	}
}
